
Language-independent input and output                                           2026-11-22
==========================================================================================

In Python 3, you can implement language-independent input and output by separating text 
from the program logic.
The principle of internationalization (i18n) is well-suited for this purpose.

1. Basic idea
   ● Program logic remains independent of the language.
   ● Input and output texts are stored in language files.
   ● The desired language is selected at runtime.
   ● New languages ​​can be added without changes to the program logic.

2. Example using Python 3
   File structure:
   ● projekt/
     ├── main.py
     └── languages/
         ├── de.json
         ├── en.json
         └── ..
   ● languages/de.json
     {
        "welcome":  "Willkommen!",
        "name":     "Wie heißt du?",
        "hello":    "Hallo {name}!",
        "exit":     "Auf Wiedersehen!"
     }
   ● languages/en.json
     {
        "welcome":  "Welcome!",
        "name":     "What is your name?",
        "hello":    "Hello {name}!",
        "exit":     "Goodbye!"
     }
   ● main.py
     import json
     from pathlib import Path

     def load_language(language):
        """Texte unabhängig von der Programmlogik lesen."""
        filename = Path("languages") / f"{language}.json"

        """
          json.load() ist eine Funktion aus dem in Python integrierten json-Modul. 
          Sie liest JSON-Daten aus einer Datei ein und wandelt diese in das 
          entsprechende Python-Objekt um.
        """  
        with filename.open(encoding="utf-8") as file:
            return json.load(file)

     def get_input(prompt):
        """Eingabe unabhängig von der Programmlogik."""
        return input(prompt)

     def show_output(message):
        """Ausgabe unabhängig von der Programmlogik."""
        print(message)
    
    def main():
        language = input("Language (de/en): ").strip().lower()

        try:
            text = load_language(language)
        except (FileNotFoundError, json.JSONDecodeError):
            text = load_language("en")

        show_output(text["welcome"])

        name = get_input(text["name"] + " ")
        show_output(text["hello"].format(name=name))

        show_output(text["exit"])

    if __name__ == "__main__":
        main()

==========================================================================================
